package com.meida.module.pay.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.meida.common.enums.StateEnum;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ListModel;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.ApiAssert;
import com.meida.module.pay.client.entity.PayConfig;
import com.meida.module.pay.provider.mapper.PayConfigMapper;
import com.meida.module.pay.provider.response.PayResponse;
import com.meida.module.pay.provider.service.PayConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付账户配置 实现类
 *
 * @author flyme
 * @date 2019-06-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PayConfigServiceImpl extends BaseServiceImpl<PayConfigMapper, PayConfig> implements PayConfigService {
    /**
     * 缓存
     */
    private final static Map<Long, PayResponse> payResponses = new HashMap();


    /**
     * 获取支付响应
     *
     * @param id 账户id
     * @return 支付响应
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public PayResponse getPayResponse(Long id, String handlerName) {
        PayResponse payResponse = payResponses.get(id);
        if (payResponse == null) {
            PayConfig apyAccount = getById(id);
            ApiAssert.isNotEmpty("支付账户不存在", apyAccount);
            payResponse = new PayResponse();
            spring.autowireBean(payResponse);
            payResponse.init(apyAccount,handlerName);
            payResponses.put(id, payResponse);
        }
        return payResponse;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody listEntityMap(ListModel listModel) {
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(listModel, entityClass);
        cq.select(entityClass, "payId", "payLogo", "payName", "appId");
        cq.eq(true, "payStatus", StateEnum.ENABLE.getCode());
        return baseList(cq);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<EntityMap> selectPayTypes() {
        CriteriaQuery cq = new CriteriaQuery(PayConfig.class);
        cq.select(PayConfig.class, "payId", "payName", "payLogo");
        cq.eq(true, "payStatus", StateEnum.ENABLE.getCode());
        return baseMapper.selectEntityMap(cq);
    }


    @Override
    public ResultBody setPayStatus(Long[] ids, Integer payStatus) {
        ApiAssert.isNotEmpty("主键不能为空", ids);
        UpdateWrapper product = new UpdateWrapper();
        product.set("payStatus", payStatus);
        product.in("payId", ids);
        boolean result = update(product);
        if (result) {
            return ResultBody.ok("操作成功", payStatus);
        } else {
            return ResultBody.failed("操作失败");
        }
    }

    @Override
    public ResultBody setIsTest(Long[] ids, Integer isTest) {
        ApiAssert.isNotEmpty("主键不能为空", ids);
        UpdateWrapper product = new UpdateWrapper();
        product.set("isTest", isTest);
        product.in("payId", ids);
        boolean result = update(product);
        if (result) {
            return ResultBody.ok("操作成功", isTest);
        } else {
            return ResultBody.failed("操作失败");
        }
    }


}
